
package com.rt.schedulenew.utils.netty.common;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.SocketConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import com.rt.schedulenew.utils.netty.service.NettySocketIOService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;

/**
 * netty 启动配置
 */
@Configuration
@ConditionalOnClass({NettySocketIOService.class})
@EnableConfigurationProperties({SocketIOProperties.class})
// 选择配置 如果有 rt.netty 才开启功能
@ConditionalOnProperty(prefix = "rt.netty", value = {"host"})
public class SocketIOAutoConfiguration {
    @Autowired
    private SocketIOProperties properties;

    /***
     * 初始化netty 配置参数
     * @return
     */
    // 选择配置
    @Bean
    @ConditionalOnMissingBean({NettySocketIOService.class})
    public NettySocketIOService nettySocketIOService() {
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setTcpNoDelay(true);
        socketConfig.setSoLinger(0);

        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setSocketConfig(socketConfig);
        config.setHostname(properties.getHost());
        config.setPort(properties.getPort());
        SocketIOServer socketIOServer = new SocketIOServer(config);
        return new NettySocketIOService(socketIOServer);
    }
}
